package com.hczt.xhminiapp.common.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.type.CollectionType;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * XML,JSON处理工具类
 * 依靠jackson提供pojo2json/xml转换
 */
public class JacksonUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 防止反射调用构造器创建对象
     */
    private JacksonUtil() {
        throw new AssertionError();
    }

    /**
     * 自定义日期序列化处理类
     * LocalDateTime
     * jdk8 support
     */
    public  static  class  JsonLocalDateTimeSerializer extends JsonSerializer<LocalDateTime>{
        public void serialize(LocalDateTime localDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            String localdateTimeStr = localDateTime.format(DateTimeFormatter.ISO_DATE_TIME);
            jsonGenerator.writeString(localdateTimeStr);
        }
    }
    /**
     * 自定义日期反序列化处理类
     * LocalDate
     * jdk8 support
     */
    public static class JsonLocalDateDeserializer extends JsonDeserializer<LocalDate> {
        @Override
        public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
            String str = jsonParser.getText().trim();
            return LocalDate.parse(str, DateTimeFormatter.ISO_DATE);
        }
    }
    /**
     * 自定义日期序列化类
     * LocalDate
     * jdk8 support
     */
    public static class JsonLocalDateSerializer extends JsonSerializer<LocalDate> {
        @Override
        public void serialize(LocalDate localDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            String localdateStr = localDate.format(DateTimeFormatter.ISO_DATE);
            jsonGenerator.writeString(localdateStr);
        }
    }
    /**
     * json数据转pojo
     *
     * @param jsonStr json字符串
     * @param cls     映射类型
     * @param <T>     推导类型
     * @return 推导类型json对象
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     */
    public static <T> T json2Pojo(String jsonStr, Class<T> cls) throws JsonParseException, JsonMappingException, IOException {
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return objectMapper.readValue(jsonStr, cls);
    }
    /**
     * json数据转PojoList
     *
     * @param jsonStr json数据
     * @param cls     类型
     * @param <T>     推导类型
     * @return pojoList
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     */
    public static <T> List<T> jsonArray2PojoList(String jsonStr, Class<T> cls) throws JsonParseException, JsonMappingException, IOException {
        CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(List.class, cls);
        return objectMapper.readValue(jsonStr, listType);
    }
    /**
     * pojo转json
     *
     * @param obj pojo
     * @return json字符串
     * @throws JsonProcessingException
     */
    public static String pojo2Json(Object obj) throws JsonProcessingException {
        return objectMapper.writeValueAsString(obj);
    }
    /**
     * json转listMap
     *
     * @param jsonArray jsonArray字符串
     * @return Listmap对象
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     */
    public static List<Map<String, Object>> jsonArray2ListMap(String jsonArray) throws JsonParseException, JsonMappingException, IOException {

        return objectMapper.readValue(jsonArray, new TypeReference<List<Map<String, Object>>>() {});
    }
    /**
     * json转map
     *
     * @param json json字符串
     * @return map对象
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     */
    public static Map<String, Object> json2Map(String json) throws JsonParseException, JsonMappingException, IOException {
        return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
    }
    /**
     * listMap转json
     *
     * @param listMap listMap
     * @return
     * @throws JsonProcessingException
     */
    public static String listMap2JsonArray(List<Map<String, Object>> listMap) throws JsonProcessingException {
        return objectMapper.writeValueAsString(listMap);
    }


    /**
     * 判断json字符串是否存在指定的key值
     * @param jsonStr 待判断的json字符串
     * @param key 指定的key值
     * @return 存在：true 不存在：false
     * @throws JsonProcessingException
     * @throws IOException
     */
    public static boolean hasKey(String jsonStr, String key) throws JsonProcessingException, IOException {
        return objectMapper.readTree(jsonStr).has(key);
    }

}
